package cn.edu.zafu.bigdata.Wym;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Wym_UDPClockClient {
    public static void main(String[] args) throws Exception {
        // 创建一个 DatagramSocket 实例
        DatagramSocket socket = new DatagramSocket();

        // 服务端地址，这里需要替换为服务端的实际 IP 地址
        InetAddress address = InetAddress.getByName("127.0.0.1");

        // 发送一个简单的请求消息
        byte[] requestBuffer = "Time please".getBytes();
        DatagramPacket request = new DatagramPacket(requestBuffer, requestBuffer.length, address, 12345);
        socket.send(request);

        // 接收服务端的响应
        byte[] responseBuffer = new byte[1024];
        DatagramPacket response = new DatagramPacket(responseBuffer, responseBuffer.length);
        socket.receive(response);

        // 打印出服务端的时间
        String serverTime = new String(response.getData(), 0, response.getLength());
        System.out.println("Server time is: " + serverTime);

        // 关闭 socket
        socket.close();
    }
}
